#include <bits/stdc++.h>
#include <optional>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

ListNode *reverseBetween(ListNode *head, int m, int n) {
    ListNode *l = new ListNode(0);
    l->next = head;
    ListNode *p = l;
    ListNode *q = head;
    for(int i = 0; i < m-1; ++i) {
        p = q;
        q = q->next;
    }
    // p -> q -> t -> 
    for(int i = 0; i < n-m; ++i) {
        ListNode *t = q->next;
        q->next = t->next;
        t->next = p->next;
        p->next = t;
    }
    return l->next;
}